package jym.tan.movielibrary.component.listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;

import javax.swing.JFileChooser;

import jym.tan.movielibrary.factory.ComponentFactory;

public class ShowFolderChooserListener implements ActionListener, MouseListener {

	public void actionPerformed(ActionEvent actionEvent) {
		if(actionEvent.getSource() == ComponentFactory.getAddSingleFolderButton()) {
			openSingleFolderChooser();
		}
	}

	public void mouseClicked(MouseEvent mouseEvent) {
		if(mouseEvent.getSource() == ComponentFactory.getAddSingleFolderTextField()) {
			openSingleFolderChooser();
		}
	}

	public void mousePressed(MouseEvent e) {
		
	}

	public void mouseReleased(MouseEvent e) {
		
	}

	public void mouseEntered(MouseEvent e) {
		
	}

	public void mouseExited(MouseEvent e) {
		
	}
	
	private void openSingleFolderChooser() {
		ComponentFactory.getSingleFolderChooser().updateUI();
		int returnValue = ComponentFactory.getSingleFolderChooser().showOpenDialog(ComponentFactory.getSingleMovieAddDialog());
		if (returnValue == JFileChooser.APPROVE_OPTION) {
            File file = ComponentFactory.getSingleFolderChooser().getSelectedFile();
            ComponentFactory.getAddSingleFolderTextField().setText(file.getAbsolutePath());
            openMovieTitleConfirmDialog(file);
        }
	}
	
	private void openMovieTitleConfirmDialog(File file) {
		ComponentFactory.getMovieTitleTextField().setText(file.getName());
        ComponentFactory.getMovieTitleConfirmDialog().setVisible(true);        
	}
}
